package cn.j2e.login.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cpms.domain.SysUser;
import com.cpms.dto.LoginUser;

import cn.j2e.login.service.iter.IUserService;
import cn.j2e.security.LogonContext;
import cn.j2web.core.commons.ResponseResult;
import cn.j2web.core.commons.ResponseResultUtil;
import cn.j2web.core.exceptions.ServiceException;


@Controller
public class MainController {
	
	private static  Logger log =LoggerFactory.getLogger(MainController.class);
	
	@Resource(name="userService")
	private IUserService userService;
	
	
	@RequestMapping(value="/menu.do")
	@ResponseBody
	public ResponseResult menu(HttpServletRequest req){
		ResponseResult responseResult=null;
		try{
			 SysUser user = new SysUser();
			 LoginUser  loginUser = LogonContext.getInstance().getLoginUser(req.getSession().getId());
			 user.setUserName(loginUser.getUserName());
			 String appId = req.getParameter("appId");
			 Object obj =  userService.qryMenusByAppIdUserId(user,appId);
			 responseResult =ResponseResultUtil.createResponse(true,"查询成功！", obj);
		} catch (ServiceException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			responseResult =ResponseResultUtil.createResponse(false,e.getMessage(), e.getMessage());
		}
		return responseResult;
	}
	
	
	@RequestMapping(value="/sys.do")
	@ResponseBody
	public List<Object> sys(HttpServletRequest req){
		
		List<Object>  sysList =new ArrayList<Object>();
		try{
			 SysUser user = new SysUser();
			 LoginUser  loginUser = LogonContext.getInstance().getLoginUser(req.getSession().getId());
			 user.setUserName(loginUser.getUserName());
			 return (List<Object>) userService.qrySysInfoByUserId(user);
		} catch (ServiceException e) {
			log.error("Exception",e);
		}
		return sysList;
	}

}
